from IPython.display import IFrame
IFrame("https://drive.google.com/file/d/1I3D9MangVoBvsyede4e3SRC_h5HbiyYh/preview", width="640", height="480")
My name is Ani Kannal
The first language I learnt was Fortran
I was very good at C, C++, Jess, Lisp, Prolog
I was mildly good at Java
I hadn't coded in many years and I was missing it. So I tried again and it was painful!
I had a big realization ...
I like the fun bits of coding but I hate getting stuck
I want to do things fast
I like quick feedback, not spending hours to realize I was doing something wrong
I would rather focus on my problem
- Go watch Netflix if you want to chill!
Anaconda
Jupyter Notebook
xcelerator.ninja
GitHub
Regular Expressions and Formatting
Machine Learning using Python
from IPython.display import IFrame
IFrame("https://drive.google.com/file/d/1oiPcoXac54AeuMgNgfL_mEkizIexBFT-/preview", width="640", height="480")
# You can use python like a calculator
# What are the different operators?
2+3
4*5
#2,3,4 operands and + * operators
#complex expressions
7*2-8**2/4
#Some more expressions
x = 5
x = 1 + 2 * 3 - 4 / 5 ** 6
print(x)
#How about some brackets?
#x = 1 + (2 * 3) - 4 / (5 ** 6) #A
#x = (1 + 2) * 3 - 4 / (5 ** 6) #B
#x = (1 + 2 * 3) - 4 / (5 ** 6) #C
#print(x)
#Other operators
#** %
# / vs // - discuss examples
2**2
9%2
7//2
# Comments start with a hash
# what is a variable?
# You can define different type of variables
x = 2
name = "Python"
# print(name)
# print(type(name))
# price = 4.5
name = 73
# print(name)
# print(type(name))
#To print a value use print
#print(x)
#print(new_str)
#print(price)
#To know what kind of data a variable has use type
# print(x)
type(x)
#help(x)
# print(type(name))
# print(type(price))
# print(type(print))
# #data types in python
# #int, float, str, bool
#print(type(True))
# #using snake case to name variables
# #cant start with number but can end with it
y = x
y = 4
print(id(x))
print(id(y))
# 2**4
#Operate upon these variables
# y = x + 4
# print(y)
# name = "Python"
# fullname = name + " Language"
# print(fullname)
price = 100
x = 2
amount = price*x
print(amount)
#More examples - variables in python
# h = 5
# w = 2
# a = h * w
# print(a)
# Use meaningful variable names
# height = 60
# width = 40
# area_of_rectangle = height * width
# print(area_of_rectangle)
# Resolving variable values
x = 2
x = 4 * x * ( 1 - x )
print(x)
print(x**2 + 4 * 20)
#Operators can behave differently on different data types
print(3+4)
print("hi " + "ani!")
print(4 + "I live in house number") #will this work? If yes, why? If not, why not?
#Converting types
#Convert to int
# inp = input('Indian floor? ')
# usf = int(inp) + 1
# print ('US floor ', usf)
def func_tempreture():
inp = input('Tempreture in Celcius ')
#print(type(inp))
# isalpha(inp)
if inp.isalpha():
far = "Error"
else:
far = float(inp)*2 + 32
print ('Tempreture in Farenheit '+ str(far))
func_tempreture()
def func_area(radius):
pi = 3.14
area = pi*radius**2
return area
func_area(3)
#function - encapsulate code
#class - encapsulates code and data
dir(str)
help(str)
#internal functions
string = "xcelerator"
print(len(string))
max(string)
min(string)
print(string)
type(string)
## Function overloading
def len(x):
print(x)
print(len(string))
len(5)
#Read from user
nam = input('Who are you? ')
print ('Welcome ', nam, end = '\t')
print ('good to see you')
#help(print)
#read(print)
# a
# b
# c
#using modules and packages
#import math
#from math import *
#dir(math)
# from math import pi
# area = pi * 7**2
# print(area)
https://wiki.python.org/moin/UsefulModules#Scientific
we will create our own module later
What have we done till now?
A few questions/comments -
Great response to the quizzes and assignments overall. I hope you are all doing them.
Agenda for today
from IPython.display import IFrame
IFrame("https://drive.google.com/file/d/19dQn0Sg4IzCNjyzBkXg5mGt2h20itcrI/preview", width="640", height="480")
# Exception handling
try:
height = float(input("Enter Height "))
width = float(input("Enter Width "))
area = height*width
print("Area:" + str(area))
except:
print("please enter a float or int value")
try:
height = float(input("Enter Height "))
width = float(input("Enter Width "))
area = height*width
print("Area:",area)
except:
print("please enter a float or int value")
#Boolean values
1 == 1
1 > 2
#type(1==1)
#type(true)
# x = 3
# y = 4
# x != y # x is not equal to y
# x > y # x is greater than y
# x < y # x is less than y
# x >= y # x is greater than or equal to y
# x <= y # x is less than or equal to y
# x is y # x is the same as y
# x is not y # x is not the same as y
#Lets do a couple of examples of conditional execution
#Lets write these conditional execution statements together.
x = input('X')
y = input('Y')
if x == y :
print('equal')
else:
if x < y:
print ('less')
else:
print ('greater')
#Indefinite loops - while
i = 0
while i < 5:
print(i)
i = i + 1
if (i==3):
break
#Definite loops - for
# for i in range(5):
# print(i)
# i = 1
# print(id(i))
# i=2
# print(id(i))
# Continue and Break
done_flag = 0
while True:
try:
height = float(input("Enter Height "))
width = float(input("Enter Width "))
break
except:
print("invalid input format, please enter numbers")
area = height*width
print("Area:" + str(area))
# Defining your own functions
# def is a keyword that indicates that this is a function definition. The name of the function is print_name.
# You can't use a keyword as the name of a function, and you should avoid having a variable and a function with the same name.
# The empty parentheses after the name indicate that this function doesn't take any arguments.
# The first line of the function definition is called the header; the rest is called the body.
# The header has to end with a colon and the body has to be indented.
# By convention, the indentation is always four spaces. The body can contain any number of statements.
# def print_name():
# name = input('What is your name? ')
# print("My name is", name)
# print_name()
# print(type(print_name))
#what are void functions?
#void function vs. return
import math
def new_function(num):
mult_pi = num*math.pi
return mult_pi
def void_function(num1, num2):
var = num1*num2
# print(var)
output = new_function(var)
# print(output)
# out_void = void_function(2,2)
output = print("abc")
print(output)
#type(new_function)
var = input("input value ")
type(var)
# mac - cmd + /
# win - ctrl + /
def func_1(num):
total = 0
total = total+num
return total
func_1(5)
func_1(3)
class Person:
name = 'no name yet'
age = 0.0
def __init__(self, name_inp, age_inp):
self.name = name_inp
self.age = age_inp
def increment(self):
self.age = self.age + 1
#s1 = Person("Ani",2)
#s2 = Person("Piyush",20)
print(s2.name)
print(s2.age)
s2.increment()
print(s2.age)
Session 3
A few questions/comments -
Great response to the quizzes and assignments overall. I hope you are all doing them.
Agenda for today
from IPython.display import IFrame
IFrame("https://drive.google.com/file/d/1pg92rAdRrmrrRa3DO-Rq6dl0iDdYI4pD/preview", width="640", height="480")
## Strings!!
b = "Hello, World!"
print(b)
b = 'Hello, World!'
print(b)
c = 'My name\ns Ani'
print(c)
# c = "My name's Ani \\\\"
# print(c)
## Strings are arrays
b = "Hello, World!"
b[5]
# # Slicing strings
a = b[2:5]
print(a)
a = b[2:-2]
print(a)
a = b[1:8:3]
print(a)
## String Functions
# len()
# strip()
# rstrip()
# lstrip()
# lower()
# upper()
# replace()
# split()
a = "Hello, World!"
# len(a)
# for i in range(len(a)):
# char = a[i]
# print (char)
# print(a)
#print(a.replace("o", "a"))
b = a.split()
b
# print(a.upper())
# #" ".join(b)
## String Operators + in
# string = 'Hello' + ' ' + 'World!'
# print(string)
## Checking for string contents
# txt = "The rain in Spain stays mainly in the plain"
# x = "ainy" in txt
# print(x)
string = "abc ncmm ammdm abcgmail.com ancn ancn \\n"
#print(string)
print(string[:])
print(string[::-1])
# print(string.reverse())
# print("@" in string)
# #dir(str)
#dir (str)
There are four collection data types in the Python programming language:
When choosing a collection type, it is useful to understand the properties of that type.
## Lists in Python []
# List is a collection which is ordered and changeable. Allows duplicate members.
lst1 = [1,2,3,4,5,3,3,2,6]
lst2 = ['abc','def','ghi']
lst3 = [1,'abc',2.3,True]
#print(lst1[::-1])
# lst_temp = lst1.reverse()
# print(lst1)
#All access mechanisms remain the same
# 2+2
# '2'+'2'
# lst_new = lst1*2
# print(lst_new)
# #Looping through lists
#func_add() - map()
for i in range(len(lst1)):
lst1[i]+=2
print(lst1)
#Checking if item exists
# Manipulating lists
# # Append
# thislist = ["apple", "banana", "cherry"]
# thislist.append("orange")
# print(thislist)
# Insert
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, ["orange","banana"])
print(thislist)
# # # Remove
# thislist = ["apple", "banana", "cherry","banana"]
# thislist.remove("banana")
# print(thislist)
# # Delete
# thislist = ["apple", "banana", "cherry","banana"]
# del thislist[0]
# print(thislist)
# print(thislist[0])
# # Pop
# thislist = ["apple", "banana", "cherry"]
# print(thislist.pop(1))
# print(thislist)
# # Clear
# thislist = ["apple", "banana", "cherry"]
# thislist.clear()
# print(thislist)
# # Join two lists
# list1 = ["a", "b" , "c"]
# list2 = [1, 2, 3]
# list3 = list1 + list2
# print(list3)
# list1.extend(list2)
# print(list1)
# How are lists different from strings?
string = 'abcd'
lst = ['a','b','c','d']
# lst[1] = 'z'
# print(lst)
# new_string = string.replace('b','z')
# print(string)
# print(new_string)
# matrix_1 = [[1,2,3],[4,5,6],[7,8,9]]
# print(matrix_1)
lst = list((1,2,3))
print(lst)
#Function Parameters
var = [5]
def change_var(func_var):
func_var[0] = 3
change_var(var)
print(var)
# what if we used a list?
# Passing a function as a parameter
## Tuples ()
# A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.
#dir(list)
#dir(tuple)
# Why use Tuples?
# Immutable
# var = (5,2)
# def change_var(func_var):
# func_var[0] = 3
# change_var(var)
# print(var)
# Return multiple values
def func_tuple(num):
return (num-1, num+1)
num1 = func_tuple(4)
print(num1[0],num1[1])
string1 = "abc"
string2 = "123"
string3 = "abc123"
print(string1.isalpha())
print(string2.isalpha())
print(string3.isalnum())
## Sets {}
# A set is a collection which is unordered and unindexed. Does not allow for duplicate members.
# You cannot access items in a set by referring to an index, since sets are unordered the items has no index.
# Access items using 'for' loop or 'in'
fruitset = {'apple', 'banana', 'guava','guava'}
#fruitset = set(['apple', 'banana', 'guava','guava'])
#fruit_lst = list(fruitset)
fruitset.add('orange')
#update
fruitset.update(['papaya', 'mango'])
#print(fruitset)
# #remove - gives error if item does not exist
# fruitset.remove('papaya')
# print(fruitset)
# # discard - does not give error
# fruitset.discard('banana')
# print(fruitset)
# union, intersection, ... all set operations
fruitset1 = {'orange','banana'}
print(fruitset.intersection(fruitset1))
print(fruitset)
## Dictionaries {}
# A dictionary is a collection which is unordered, changeable and indexed.
# In Python dictionaries are written with curly brackets, and they have keys and values.
inventory_dict = {
"apples": 4,
"oranges": 7,
"guavas": 2
}
# print(inventory_dict)
# lst_dict = inventory_dict.items() #returns a list of tuples
# # for key in inventory_dict:
# # print (inventory_dict[key])
# inventory_dict["apples"] = inventory_dict["apples"]-1
# print(inventory_dict["apple"])
# print(inventory_dict.values())
inventory_dict["apple"]
# working with dictionaries -
# check if key exists -
'apples' in inventory_dict
# accessing data
print(inventory_dict.get("apple",0))
# changing values
#car_dict["brand"] = "Toyota"
a = {'a':2, "a":4}
n = a['a']
print(a)
# random number - 2401
# #user input - 1307
# 1 - cow
# 1 - bull
# 1234
# 0 - cows
# 3 - bulls
1
# Generate a random number
# split random number into four digits
# user input - split that into four digits
check_list = []
for i in '2401':
check_list.append(i in '1234')
check_list
How to ask for help from Python? dir() and help()
Exception handling
Session 4
from IPython.display import IFrame
IFrame("https://drive.google.com/file/d/1do06kYxwj-YygZcp0phRW8Kz0ozSBBi0/preview", width="640", height="480")
# map function in python
def double_func(num):
return num*2
num_lst = (1,2,3,4)
dbl_lst = []
# for i in num_lst:
# dbl_lst.append(double_func(i))
dbl_lst
dbl_lst = map(double_func, num_lst)
print(type(dbl_lst))
print(list(dbl_lst))
#List comprehension
user_input = input("input number ")
sq_lst_all = [num**2 for num in num_lst]
sq_lst_even = [num**2 for num in num_lst if num%2==0]
print(sq_lst_all)
print(sq_lst_even)
# filter function in python
# function that filters vowels
def check_vowel(variable):
vowels = ['a', 'e', 'i', 'o', 'u']
if (variable in vowels):
return variable
else:
return False
# sequence
sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r']
# using filter function
filtered = filter(check_vowel, sequence)
list(filtered)
print(type(filter))
sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r']
vowels = ['a', 'e', 'i', 'o', 'u']
vowel_lst = [letter for letter in sequence if letter in vowels]
vowel_lst
# Live exercise: Work with strings and lists - find the first email id in a given string
str1 = 'abcd ajdjd adjdj ani@gmail.com ajdjd djj ani@hotmail.com mamd'
def emailfinder_func(string):
words = string.split()
for word in words:
if word.find('@') == -1: #word does not contain @
continue
else: #word contains @
return word
emailfinder_func(str1)
help(str.find)
Double-click here for the solution.
# Live exercise: Take a string input and count occurence of each letter in the string
string = 'abcnde'
letters = list(string)
letters
def letter_counter(string):
letter_dict = dict()
#words = string.split() #list of all words in the string
for letter in string:
# check if any of the special characters are in the word
if letter != ' ':
letter_dict[letter.upper()] = letter_dict.get(letter.upper(),0)+1
return letter_dict
letter_counter('abcd ajdjd adjdj ani@gmail.com ajdjd djj ani@hotmail.com mamd cat Cat doG dog Dog')
Double-click here for the solution.
# Live exercise: Take a string input and count occurence of each word in the string
def word_counter(string):
word_dict = dict()
words = string.split() #list of all words in the string
for word in words:
# check if any of the special characters are in the word
word_dict[word] = word_dict.get(word,0)+1
return word_dict
word_counter('abcd ajdjd adjdj ani@gmail.com ajdjd djj ani@hotmail.com mamd cat Cat doG dog Dog')
"abc's".isalpha()
Double-click here for the solution.
# Live exercise: Read the code - read from a file and list top 10 words by occurence.
import urllib.request
target_url = 'https://raw.githubusercontent.com/anikannal/PythonForMachineLearning/master/email_dump.txt'
data = urllib.request.urlopen(target_url)
text = str(data.read())
words = text.split()
counts = dict()
for word in words:
counts[word] = counts.get(word,0)+1
countlist = []
for word,count in counts.items():
countlist.append((count,word))
countlist.sort(reverse=True) #sorts in descending order
print (countlist[:10])
Double-click here for the solution.
# Live exercise: Read the code - read from a file and count emails from each email id.
import re
def count_message_from_email():
fhand = open('./email_dump.txt')
messages = dict()
for line in fhand:
line = line.rstrip()
if line.startswith('From'): #re.search('^From',line): #check if line starts with From
split_line = line.split()
messages[split_line[1]] = messages.get(split_line[1],0)+1
return messages
count_message_from_email()
Double-click here for the solution.